﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using AppBrowser.Common;

namespace AppBrowser
{
    public partial class frmEditCategory : Form
    {
        public string IconFile;
        public string Category
        {
            get { return this.textBox1.Text; }
            set { textBox1.Text = value; }
        }
        
        private bool _EditMode = false;
        public bool EditMode
        {
            get { return _EditMode; }
            set { _EditMode = value; }
        }
        
        public void SetMode()
        {
            if (_EditMode)
            {
                this.Text = "Edit Category";
                this.button1.Text = "Update";
            } else {
                this.Text = "Add Category";
                this.button1.Text = "Add";
            }
        }

        public frmEditCategory()
        {
            InitializeComponent();
        }

        private void frmEditCategory_Load(object sender, EventArgs e)
        {
            SetMode();
            if (textBox1.Text == "")
            {
                button1.Enabled = false;
            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            // open dialog to select icon
            OpenFileDialog fd = new OpenFileDialog();
            fd.Filter = "Icons (*.ico) |*.ico;|(*.*) |*.*";
            string iconFile;

            if (fd.ShowDialog() == DialogResult.OK)
            {
                // now process file, 
                if (Path.GetDirectoryName(fd.FileName) == AppData.AppCategoryPath)
                {
                    iconFile = fd.FileName;
                }
                else
                {
                    // if not in category directory then copy it to there
                    // diff dir, load icon data and save in category path
                    iconFile = AppData.AppCategoryPath + "\\" + Path.GetFileName(fd.FileName);
                    bool copyit = true;

                    if (File.Exists(iconFile))
                    {
                        if (MessageBox.Show("Icon file : " + Path.GetFileName(fd.FileName) + " exists in categories directory. Do you want to overwrite it?",
                            "Replace file? Yes/No", MessageBoxButtons.YesNo) == DialogResult.Yes)
                        {
                            copyit = true;
                        }
                        else
                        {
                            return;
                        }
                    }

                    // copy icon file
                    if (copyit && (Path.GetExtension(fd.FileName).ToLower() == ".ico"))
                    {
                        File.Copy(fd.FileName, iconFile, true);
                    }
                    else
                    {
                        // not .ico file so load in data
                        // IconFile = IconExtract.ExtractIconFromFile(fd.FileName);
                    }
                }

                IconFile = iconFile;

                // now set picturebox
                pictureBox1.Image = IconExtract.ExtractIconFromFile(IconFile).ToBitmap();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
            {
                button1.Enabled = true;
            }
            else { button1.Enabled = false; }
        }
    }
}
